package com.huazai.controller;

import com.huazai.bean.po.Books;
import com.huazai.bean.po.Borrow;
import com.huazai.bean.po.User;
import com.huazai.bean.vo.BorrowVO;
import com.huazai.service.BookService;
import com.huazai.service.BorrowService;
import com.huazai.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理借阅申请
 * @author EZTTU
 */
@Controller
@Transactional
@RequestMapping("/borrow")
public class BorrowController {

    private static final String STATUS_PREPARED = "prepared";
    private static IdUtil idUtil;

    static {
        idUtil = new IdUtil(1, 1, 1);
    }

    private BorrowService borrowService;

    private BookService bookService;

    @Autowired
    @Qualifier("BookServiceImpl")
    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }

    @Autowired
    @Qualifier("BorrowServiceImpl")
    public void setBorrowService(BorrowService borrowService) {
        this.borrowService = borrowService;
    }

    @RequestMapping("/toAllBorrow")
    public String toAllBorrow(Model model) {

        List<BorrowVO> borrowVOList = borrowService.getBorrowCustom(null);
        model.addAttribute("borrowVOList", borrowVOList);
        return "borrow/allBorrow";
    }

    @RequestMapping("/toMyBorrow")
    public String toMyBorrow(@RequestParam("borrowerEmail") String borrowerEmail,
                             Model model) {

        List<BorrowVO> borrowVOList = borrowService.getBorrowCustom(borrowerEmail);
        model.addAttribute("borrowVOList", borrowVOList);
        return "borrow/allBorrow";
    }


    @RequestMapping("/queryApply")
    public String queryApply(@RequestParam("borrowerEmail") String borrowerEmail,
                             Model model) {

        List<BorrowVO> borrowVOList = borrowService.getBorrowCustom(borrowerEmail);
        model.addAttribute("borrowVOList", borrowVOList);

        if (borrowVOList.isEmpty()) {
            model.addAttribute("queryMessage", "未查到指定申请");
        } else {
            model.addAttribute("queryMessage", "已查到指定申请");
        }
        return "borrow/allBorrow";
    }

    @RequestMapping("/deleteApply")
    public String deleteApply(@RequestParam("borrowID") String borrowID) {

        borrowService.restoreBookCount(borrowID);
        borrowService.deleteBorrowApply(borrowID);

        return "redirect:/borrow/toAllBorrow";
    }

    @RequestMapping("/deleteMyApply")
    public String deleteMyApply(@RequestParam("borrowID") String borrowID,
                                HttpSession session) {
        User userMessage = (User) session.getAttribute("userMessage");

        borrowService.restoreBookCount(borrowID);
        borrowService.deleteBorrowApply(borrowID);

        return "redirect:/borrow/toMyBorrow?borrowerEmail=" + userMessage.getEmail();
    }


    @RequestMapping("/approveApply")
    public String approveApply(@RequestParam("borrowID") String borrowID,
                               @RequestParam("borrowStatus") String borrowStatus) {
        Map<String, String> map = new HashMap<>();
        map.put("borrowID", borrowID);
        map.put("borrowStatus", borrowStatus);
        borrowService.approveBorrowApply(map);

        return "redirect:/borrow/toAllBorrow";
    }

    @RequestMapping("/generalSubmit")
    public String generalSubmit(Borrow borrow,
                                Model model) {
        System.out.println("borrow = " + borrow);

        borrow.setBorrowID(idUtil.nextId());
        Long bookID = borrow.getBookID();

        Books book = bookService.selectBook(bookID, null).get(0);
        Integer bookCounts = book.getBookCounts();
        bookCounts --;
        book.setBookCounts(bookCounts);

        borrow.setBorrowStatus(STATUS_PREPARED);
        borrow.setCost(borrow.getDuration() * book.getPrice());

        bookService.updateBook(book);
        borrowService.addBorrowApply(borrow);
        model.addAttribute("result", "申请成功");
        return "user/message";
    }

}
